package com.flightgame.scenarios;

import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.transaction.TransactionConfiguration;
import org.springframework.transaction.annotation.Transactional;
import com.flightgame.hibernate.FlightDao;
import com.flightgame.hibernate.OwnershipDao;
import com.flightgame.hibernate.User;
import com.flightgame.hibernate.UserDao;
import com.flightgame.services.FlightServices;
import com.flightgame.services.GameServices;
import com.flightgame.services.JSONServices;
import com.flightgame.tools.Utils;


@RunWith(SpringJUnit4ClassRunner.class)
@Transactional
@TransactionConfiguration(transactionManager="hibernateTransactionManager")
@ContextConfiguration(locations={"classpath:/applicationContext-flightgame.xml"})
public class Scenarios {
	@Autowired
	private GameServices gameServices;
	@Autowired
	private FlightServices flightServices;
	@Autowired
	private JSONServices jsonServices;
	@Autowired
	private OwnershipDao ownershipDao;
	@Autowired
	private FlightDao flightDao;
	@Autowired
	private UserDao userDao;
	
	
	@Test
	public void game1(){
		gameServices.addNewUser("cagzilla","");
		gameServices.addNewUser("caglar08","");
		
		gameServices.buyItem("caglar08","aircraft1",0,0,"ESB");
		Assert.assertEquals("aircraft1", ownershipDao.getUserOwnerships("caglar08").get(0).getItemname());
		int ownershipId_cagzilla = ownershipDao.getUserOwnerships("caglar08").get(0).getId();
		
		Assert.assertEquals(100,flightServices.getFuelOfAircraft(ownershipId_cagzilla));
		
		gameServices.buyItem("cagzilla","gasstation1",50,50,"");
		int ownershipId_gasStation = ownershipDao.getUserOwnerships("cagzilla").get(0).getId();
		Assert.assertEquals("gasstation1", ownershipDao.getUserOwnerships("cagzilla").get(0).getItemname());
		
		flightServices.decreaseIncreaseFuelOfAircraft(ownershipId_cagzilla, -1);
		Assert.assertEquals(99,flightServices.getFuelOfAircraft(ownershipId_cagzilla));
		
		flightServices.createFlight("caglar08","aircraft1", ownershipId_cagzilla);
		
		flightServices.refreshFlightPosition(ownershipId_cagzilla, 44, 44, 300);
		Assert.assertEquals(44,(int) flightServices.getFlight(ownershipId_cagzilla).getLat());
		
		flightServices.decreaseIncreaseFuelOfAircraft(ownershipId_cagzilla, -9);
		Assert.assertEquals(90,flightServices.getFuelOfAircraft(ownershipId_cagzilla));
		
		flightServices.buyFuelToAircraft(ownershipId_cagzilla, ownershipId_gasStation, 15);
		
		Assert.assertEquals(100,flightServices.getFuelOfAircraft(ownershipId_cagzilla));
		User user1 = userDao.getUser("caglar08");
		User user2 = userDao.getUser("cagzilla");
		
		Assert.assertEquals(899900,user1.getCredits());
		Assert.assertEquals(500100,user2.getCredits());
		
		
	}
}
